//
//  RBCommerceTabBarVC.m
//  CommerceApp
//
//  Created by Lan on 2018/2/22.
//  Copyright © 2018年 SummerTea. All rights reserved.
//

#import "RBCommerceTabBarVC.h"
#import "NSObject+YYAddForKVO.h"
#import "NSObject+YYAdd.h"
#import <YYCategories/UIView+YYAdd.h>
#import "RBDefine.h"
#import "RBAppDelegateHelper.h"
#import "UIButton+RBAdd.h"

#define kMenuBtnHeight (92.0/750)*kRBMainScreenWidth

@interface RBCommerceTabBarVC ()

@property(nonatomic, strong) UIButton *responseBtn;
@property(nonatomic, strong) UIImageView *displayView;

@end

@implementation RBCommerceTabBarVC

#pragma mark - Lifecycle

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
    {
        kRBWeakSelf
        [self.tabBar addObserverBlockForKeyPath:@"hidden" block:^(id  _Nonnull obj, id  _Nonnull oldVal, id  _Nonnull newVal) {
            [weak_self setupResponseBtn];
        }];
    }
    return self;
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self hideResponseBtn];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self showResponseBtn];
}

- (void)dealloc
{
    [self.tabBar removeObserverBlocks];
    [self.responseBtn removeFromSuperview];
    self.responseBtn = nil;
}

#pragma mark - Private

- (void)layOutSubviewsOnTabBar
{
    [super layOutSubviewsOnTabBar];
    [self.tabBar bringSubviewToFront:self.displayView];
    self.displayView.centerX = self.tabBar.width/2;
    self.displayView.centerY = 0;
    [[RBAppDelegateHelper keyWindow] bringSubviewToFront:self.responseBtn];
    self.responseBtn.frame = [self.tabBar convertRect:self.displayView.frame toView:[RBAppDelegateHelper keyWindow]];
}

- (void)setupResponseBtn
{
    self.responseBtn.hidden = self.tabBar.hidden;
    if (self.tabBar.hidden == NO)
    {
        [[RBAppDelegateHelper keyWindow] bringSubviewToFront:self.responseBtn];
    }
}

#pragma mark - Public

- (void)hideResponseBtn
{
    self.responseBtn.hidden = YES;
}

- (void)showResponseBtn;
{
    UINavigationController *currentNavi = (UINavigationController *)self.selectedViewController;
    if ([currentNavi isKindOfClass:[UINavigationController class]])
    {
        if (currentNavi.viewControllers.count <= 1)
        {
            self.responseBtn.hidden = NO;
        }
    }
}

#pragma mark - Event

- (void)menuBtnTapped
{
    NSArray *naviControllers = self.viewControllers;
    for (UINavigationController *naviVC in naviControllers)
    {
        if (![naviVC isKindOfClass:[UINavigationController class]] || naviVC.viewControllers.count > 1)
        {
            NSLog(@"屏蔽下乱点");
            return;
        }
    }
    //操作
    NSLog(@"操作");
}

#pragma mark - Getter

- (UIButton *)responseBtn
{
    if (!_responseBtn)
    {
        self.responseBtn = ({
            UIButton *btn = [[UIButton alloc] init];
            [[RBAppDelegateHelper keyWindow] addSubview:btn];
            btn.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
            [btn rbAddTarget:self action:@selector(menuBtnTapped)];
            btn;
        });
    }
    return _responseBtn;
}

- (UIImageView *)displayView
{
    if (!_displayView)
    {
        self.displayView = ({
            UIImageView *view = [UIImageView new];
            view.backgroundColor = [UIColor redColor];
            [self.tabBar addSubview:view];
            view.width = kMenuBtnHeight;
            view.height = kMenuBtnHeight;
            view;
        });
    }
    return _displayView;
}

@end
